// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //
//  »Project«   Talina Gaming System (TgS) (∂)
//  »File«      TgS Common - Base - Type [File].c
//  »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
//  »Version«   4.0
// ------------------------------------------------------------------------------------------------------------------------------ //
//  Copyright: © 2002-2010, Andrew Aye.  All Rights Reserved.
//  This software is free for non-commercial use. Redistribution and use in source and binary forms, with or without modification,
//  are permitted provided that the following conditions are met: 
//    Redistributions of source code must retain this copyright notice, this list of conditions and the following disclaimers. 
//    Redistributions in binary form must reproduce this copyright notice, this list of conditions and the following
//      disclaimers in the documentation and other materials provided with the distribution. 
//  Neither the names of the copyright owner nor the names of its contributors may be used to endorse or promote products derived
//  from this software without specific prior written permission. 
//  The intellectual property rights of the algorithms used reside with Andrew Aye.  You may not use this software, in whole or
//  in part, in support of any commercial product without the express written consent of the author.
//  There is no warranty or other guarantee of fitness of this software for any purpose. It is provided solely "as is".
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= //


// START TGS - PATH ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// ---- tgCM_Path_Add_Seperator ---------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------------------------------ //
P_TgCHAR tgCM_Path_Add_Seperator( PC_TgCHAR pszDest, C_TgUINT32 uiLength )
{
    C_TgUINT32                              nuiDest = tgSZ_Length( pszDest );

    TgASSERT( 0 != pszDest );

    if ((0 == nuiDest) || (nuiDest >= uiLength - 2))
    {
        return (0);
    };

    if (TgCHAR_PATH_SEPERATOR == pszDest[nuiDest - 1])
    {
        return (pszDest);
    };

    pszDest[nuiDest + 0] = TgCHAR_PATH_SEPERATOR;
    pszDest[nuiDest + 1] = 0;
    return (pszDest);
}


// ---- tgCM_Path_Add_Extension ---------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------------------------------ //
P_TgCHAR tgCM_Path_Add_Extension( PC_TgCHAR pszDest, C_TgUINT32 uiLength, CPC_TgCHAR pszExt )
{
    C_TgUINT32                              nuiDest = tgSZ_Length( pszDest );
    C_TgUINT32                              nuiExt = tgSZ_Length( pszExt );

    TgASSERT( (0 != pszDest) && (0 != pszExt) );

    if (nuiDest + nuiExt + 1 >= uiLength - 1)
    {
        return (0);
    };

    if ('.' != pszExt[0])
    {
        tgSZ_Append( pszDest, uiLength, TgT(".") );
    };

    tgSZ_Append( pszDest, uiLength, pszExt );

    return (pszDest);
}


// ---- tgCM_Path_Copy ------------------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------------------------------ //
P_TgCHAR tgCM_Path_Copy( PC_TgCHAR pszDest, C_TgUINT32 uiLength, CPC_TgCHAR pszAdd )
{
    TgASSERT( (0 != pszDest) && (uiLength > 1) );
    pszDest[0] = 0;
    return (tgCM_Path_Append( pszDest, uiLength, pszAdd ));
}


// ---- tgCM_Path_Append ----------------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------------------------------ //
P_TgCHAR tgCM_Path_Append( PC_TgCHAR pszDest, C_TgUINT32 uiLength, CPC_TgCHAR pszAdd )
{
    C_TgUINT32                              nuiDest = tgSZ_Length( pszDest );
    C_TgUINT32                              nuiAdd = tgSZ_Length( pszAdd );
    TgBOOL                                  bPrefix_Sep;

    TgASSERT( (0 != pszDest) && (uiLength > 1) );

    if (nuiDest + nuiAdd + 2 > uiLength)
    {
        return (0);
    };

    bPrefix_Sep = TgCHAR_PATH_SEPERATOR == pszAdd[0];

    if (nuiDest > 0)
    {
        tgCM_Path_Add_Seperator( pszDest, uiLength );
        tgSZ_Append( pszDest, uiLength, pszAdd + (bPrefix_Sep ? 1 : 0) );
    }
    else
    {
        tgSZ_Copy( pszDest, uiLength, pszAdd );
    };

    return (pszDest);
}


// ---- tgCM_Path_Common_Prefix ---------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------------------------------ //
P_TgCHAR tgCM_Path_Common_Prefix( PC_TgCHAR pszDest, C_TgUINT32 uiLength, CPC_TgCHAR pszPath0, CPC_TgCHAR pszPath1 )
{
    TgUINT32                                uiIndex = 0;

    TgASSERT( (0 != pszDest) && (0 != pszPath0) && (0 != pszPath1) );

    for (; (pszPath0[uiIndex] == pszPath1[uiIndex]) && (0 != pszPath0[uiIndex]) && (0 != pszPath1[uiIndex]); ++uiIndex)
    {
        pszDest[uiIndex] = pszPath0[uiIndex];
    };

    pszDest[uiIndex] = 0;
    return (pszDest);
}


// ---- tgCM_Path_Remove_Seperator ------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------------------------------ //
P_TgCHAR tgCM_Path_Remove_Seperator( PC_TgCHAR pszDest )
{
    C_TgUINT32                              nuiDest = tgSZ_Length( pszDest );

    TgASSERT( 0 != pszDest );

    if (nuiDest < 1)
    {
        return (pszDest);
    };

    if (TgCHAR_PATH_SEPERATOR != pszDest[nuiDest - 1])
    {
        return (pszDest);
    };

    pszDest[nuiDest - 1] = 0;
    return (pszDest);
}


// ---- tgCM_Path_Remove_Extension ------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------------------------------ //
P_TgCHAR tgCM_Path_Remove_Extension( PC_TgCHAR pszDest )
{
    PC_TgCHAR pszExt = tgCM_Path_Find_Extension( pszDest );

    if (0 != pszExt)
    {
        pszExt[0] = 0;
        return (pszDest);
    }

    return (pszDest);
}


// ---- tgCM_Path_Remove_File_Name ------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------------------------------ //
P_TgCHAR tgCM_Path_Remove_File_Name( PC_TgCHAR pszDest )
{
    C_TgUINT32                              nuiDest = tgSZ_Length( pszDest );
    TgUINT32                                uiIndex = nuiDest;

    TgASSERT( 0 != pszDest );

    for (uiIndex = nuiDest; uiIndex > 0; --uiIndex)
    {
        if (TgCHAR_PATH_SEPERATOR == pszDest[uiIndex-1])
        {
            pszDest[uiIndex] = 0;
            return (pszDest);
        };
    };

    return (pszDest);
}


// ---- tgCM_Path_Rename_Extension ------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------------------------------ //
P_TgCHAR tgCM_Path_Rename_Extension( PC_TgCHAR pszDest, C_TgUINT32 nuiDest, CPC_TgCHAR pszExt )
{
    C_TgUINT32                              uiLength = tgSZ_Length( pszDest );
    C_TgUINT32                              nuiExt = tgSZ_Length( pszExt );
    TgUINT32                                uiIndex = nuiDest;

    TgASSERT( 0 != pszDest );

    for (uiIndex = uiLength; uiIndex > 0; --uiIndex)
    {
        if (TgT('.') == pszDest[uiIndex-1])
        {
            if (uiIndex + nuiExt + 2 > nuiDest)
            {
                return (0);
            };

            pszDest[uiIndex] = 0;

            if ('.' == pszExt[0])
            {
                tgSZ_Copy( pszDest + uiIndex, nuiDest - uiIndex, pszExt + 1 );
            }
            else
            {
                tgSZ_Copy( pszDest + uiIndex, nuiDest - uiIndex, pszExt + 0 );
            };

            return (pszDest);
        };
    };

    return (pszDest);

}


// ---- tgCM_Path_Remove_Directory ------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------------------------------ //
P_TgCHAR tgCM_Path_Remove_Directory( PC_TgCHAR pszDest )
{
    C_TgUINT32                              nuiDest = tgSZ_Length( pszDest );
    TgUINT32                                uiIndex = nuiDest;

    TgASSERT( 0 != pszDest );

    for (uiIndex = nuiDest; uiIndex > 0; --uiIndex)
    {
        if (TgCHAR_PATH_SEPERATOR == pszDest[uiIndex-1])
        {
            TgUINT32                                uiCoffee;

            for (uiCoffee = 0; uiIndex <= nuiDest; ++uiIndex, ++uiCoffee)
            {
                pszDest[uiCoffee] = pszDest[uiIndex];
            };

            return (pszDest);
        };
    };

    return (pszDest);
}


// ---- tgCM_Path_Init ------------------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------------------------------ //
P_TgCHAR tgCM_Path_Init( PC_TgCHAR pszDest, C_TgUINT32 nuiDest, ... )
{
    CP_TgCHAR                               pszPath;
    va_list                                 vaList;

    TgASSERT( 0 != pszDest && 0 < nuiDest );

    va_start( vaList, nuiDest );

    pszPath = (CP_TgCHAR)va_arg( vaList, TgSINTPTR );

    *pszDest = 0;

    for (; 0 != *pszPath; pszPath = (CP_TgCHAR)va_arg( vaList, TgSINTPTR ))
    {
        C_TgUINT32                              nuitgCM_Path_Append = tgSZ_Length( pszPath );
        C_TgUINT32                              nuiPath = tgSZ_Length( pszDest );

        if (nuiPath + nuitgCM_Path_Append >= nuiDest)
        {
            return (0);
        };

        tgSZ_Append( pszDest, nuiDest, pszPath );

        if (0 == tgCM_Path_Add_Seperator( pszDest, nuiDest ))
        {
            return (0);
        };
    };

    va_end( vaList );

    return (pszDest);
}


// ---- tgCM_Path_Find_Extension --------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------------------------------ //
P_TgCHAR tgCM_Path_Find_Extension( PC_TgCHAR pszDest )
{
    C_TgUINT32                              nuiDest = tgSZ_Length( pszDest );
    TgUINT32                                uiIndex = nuiDest;

    TgASSERT( 0 != pszDest );

    for (uiIndex = nuiDest; uiIndex > 0; --uiIndex)
    {
        if ('.' == pszDest[uiIndex-1])
        {
            return (pszDest + uiIndex - 1);
        };
    };

    return (0);
}


// ---- tgCM_Path_Find_File_Name --------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------------------------------ //
P_TgCHAR tgCM_Path_Find_File_Name( PC_TgCHAR pszDest )
{
    C_TgUINT32                              nuiDest = tgSZ_Length( pszDest );
    TgUINT32                                uiIndex = nuiDest;

    TgASSERT( 0 != pszDest );

    if (TgCHAR_PATH_SEPERATOR == pszDest[nuiDest-1])
    {
        return (0);
    };

    for (uiIndex = nuiDest; uiIndex > 0; --uiIndex)
    {
        if (TgCHAR_PATH_SEPERATOR == pszDest[uiIndex-1])
        {
            return (pszDest + uiIndex);
        };
    };

    return (0);
}